package com.htzos.admin.ac.controller;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.htzos.admin.ac.bean.AcMessageBase;
import com.htzos.admin.ac.service.AcMessageBaseService;
import com.htzos.admin.system.bean.SystemRoleBase;
import com.htzos.admin.system.bean.SystemUserBase;
import com.htzos.admin.system.service.SystemRoleBaseService;
import com.htzos.admin.system.service.SystemUserBaseService;
import com.htzos.base.parent.ctrl.BaseController;
import com.htzos.common.tool.ExtQueryBean;
import com.htzos.common.tool.TldTool;
import com.htzos.common.util.CommonUtil;

@Controller
@RequestMapping("/acMessageBase")
public class AcMessageBaseController extends BaseController{

    @Resource
    AcMessageBaseService acMessageBaseService;
    
    @Autowired
    SystemRoleBaseService systemRoleBaseService;
    @Autowired
    SystemUserBaseService systemUserBaseService;

    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request,AcMessageBase condition, ExtQueryBean extQuery) {
        if(extQuery.getPageNum() == null) {
            extQuery.setSize(10);
            extQuery.setPageNum(1);
        }
        
        SystemUserBase user = (SystemUserBase) request.getSession().getAttribute(CommonUtil.CURRENTUSER);
        condition.setAuthor(user.getUserName());

        extQuery.setAppendSql(" order by id desc ");
        
        extQuery.setTotal(acMessageBaseService.acMessageBaseTotal(condition, extQuery));
        List<AcMessageBase> list= acMessageBaseService.acMessageBaseList(condition, extQuery);
        request.setAttribute("list", list);
        request.setAttribute("condition", condition);
        request.setAttribute("extQuery", extQuery);
        request.getSession(true).setAttribute("conditionStr", condition);
        request.getSession(true).setAttribute("extQueryStr", extQuery);
        return new ModelAndView("admin/ac/acMessageBaseList");
    }

    @RequestMapping("/update")
    public ModelAndView update(HttpServletRequest request, AcMessageBase acMessageBase) {
        acMessageBaseService.acMessageBaseUpdate(acMessageBase);
        return new ModelAndView(new RedirectView("list"), getSessionQueryString(request));
    }

    @RequestMapping("/updatePage")
    public String updatePage(HttpServletRequest request, String id) {
        AcMessageBase acMessageBase = acMessageBaseService.acMessageBaseGet(id);
        request.setAttribute("acMessageBase", acMessageBase);
        return "admin/ac/acMessageBaseUpdate";
    }

//    @RequestMapping("/insert")
//    public ModelAndView insert(HttpServletRequest request, AcMessageBase acMessageBase) {
//    	
//    	SystemUserBase user = (SystemUserBase) request.getSession().getAttribute(CommonUtil.CURRENTUSER);
//    	
//    	acMessageBase.setAuthor(user.getUserName());
//    	acMessageBase.setStatus(TldTool.STATUS_USE);
//    	acMessageBase.setType(1);//先都是系统消息吧
//    	
//        acMessageBaseService.acMessageBaseInsert(acMessageBase);
//        return new ModelAndView(new RedirectView("list"), getSessionQueryString(request));
//    }
    
    @RequestMapping("/insert")
    public void insert(HttpServletResponse response,HttpServletRequest request, AcMessageBase acMessageBase){
    	String resultMsg;
    	try {
    		
    		if(acMessageBase.getContent().isEmpty()||acMessageBase.getTitle().isEmpty()){
    			resultMsg="标题和内容都不能为空";
    			writeResultMessage(response, resultMsg, FAIL);
    			return;
    		}
    		
			SystemUserBase user = (SystemUserBase) request.getSession().getAttribute(CommonUtil.CURRENTUSER);
			acMessageBase.setAuthor(user.getUserName());
			acMessageBase.setStatus(TldTool.STATUS_USE);
			acMessageBase.setType(1);//先都是系统消息吧
			acMessageBaseService.acMessageBaseInsert(acMessageBase);
			resultMsg="消息下发成功";
			writeResultMessage(response, resultMsg, SUCCESS);
		} catch (Exception e) {
			resultMsg="消息下发失败";
			writeResultMessage(response, resultMsg, FAIL);
		}
    }
    
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,HttpServletResponse response) {
    	String strRoleId=request.getParameter("roleId");
    	String strUserId=request.getParameter("userId");
    	
    	AcMessageBase acMessageBase=new AcMessageBase();
    	
    	if(strRoleId==null||strRoleId.isEmpty()||strRoleId.equals("0")){
    		acMessageBase.setToroleId(0);
    		acMessageBase.setToroleName("不限制角色");
    	}else{
    		Integer roleId=Integer.parseInt(strRoleId);
    		SystemRoleBase role=systemRoleBaseService.systemRoleBaseGet(roleId);
    		acMessageBase.setToroleId(roleId);
    		acMessageBase.setToroleName(role.getRoleName());
    	}
    	
    	if(strUserId==null||strUserId.isEmpty()||strUserId.equals("0")){
    		acMessageBase.setTouserId(0);
    		acMessageBase.setTouserName("不限制用户");
    	}else{
    		
    		Integer userId=Integer.parseInt(strUserId);
    		SystemUserBase user=systemUserBaseService.systemUserBaseGet(userId);
    		acMessageBase.setTouserId(userId);
    		acMessageBase.setTouserName(user.getUserName());
    	}
    	
    	request.setAttribute("message", acMessageBase);
    	
    	
        return "admin/ac/acMessageBaseAdd";
    }

    @RequestMapping("/delete")
    public ModelAndView delete(HttpServletRequest request, AcMessageBase acMessageBase) {
        acMessageBaseService.acMessageBaseDelete(acMessageBase);
         return new ModelAndView(new RedirectView("list"), getSessionQueryString(request));
    }
}